package com.longxi.tianjintianbao.ui.activity.scan;

import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;

import butterknife.BindView;
import cn.bingoogolapple.qrcode.core.BarcodeType;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

public class ScanActivity extends BaseActivity implements  QRCodeView.Delegate {
    private static final String TAG = ScanActivity.class.getSimpleName();

    @BindView(R.id.rlBar)
    RelativeLayout mRlBar;

    @BindView(R.id.ivFlash)
    ImageView mIvFlash;
    @BindView(R.id.tvFlashTip)
    TextView mTvFlashTip;
    @BindView(R.id.zxingview)
    ZXingView mZXingView;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_scan;
    }

    @Override
    public void initData() {
        super.initData();
        mRlBar.setBackgroundColor(getResources().getColor(R.color.translucent));
        setTvLeft("返回");
        setTitle("扫一扫");

        mZXingView.setDelegate(this);
        mZXingView.changeToScanQRCodeStyle(); // 切换成扫描二维码样式
        mZXingView.setType(BarcodeType.TWO_DIMENSION, null); // 只识别二维条码
        mZXingView.startSpotAndShowRect(); // 显示扫描框，并开始识别
    }

    @Override
    public void initListener() {
        super.initListener();
        mIvFlash.setOnClickListener(v -> openCloseFlash());
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }


    @Override
    protected void onStop() {
        mZXingView.stopCamera(); // 关闭摄像头预览，并且隐藏扫描框
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mZXingView.onDestroy(); // 销毁二维码扫描控件
        super.onDestroy();
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        Log.i(TAG, "result:" + result);
//        setTitle("扫描结果为：" + result);
        vibrate();
        Intent resultIntent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putInt(AppConst.RESULT_TYPE, AppConst.RESULT_SUCCESS);
        bundle.putString(AppConst.RESULT_STRING, result);
        resultIntent.putExtras(bundle);
        setResult(RESULT_OK, resultIntent);
        finish();
//        mZXingView.startSpot(); // 开始识别
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {
        // 这里是通过修改提示文案来展示环境是否过暗的状态，接入方也可以根据 isDark 的值来实现其他交互效果
        String tipText = mZXingView.getScanBoxView().getTipText();
        String ambientBrightnessTip = "\n环境过暗，请打开闪光灯";
        if (isDark) {
            if (!tipText.contains(ambientBrightnessTip)) {
                mZXingView.getScanBoxView().setTipText(tipText + ambientBrightnessTip);
            }
        } else {
            if (tipText.contains(ambientBrightnessTip)) {
                tipText = tipText.substring(0, tipText.indexOf(ambientBrightnessTip));
                mZXingView.getScanBoxView().setTipText(tipText);
            }
        }
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Log.e(TAG, "打开相机出错");
    }

    boolean isFlash = false;
    /**
     * 开启关闭闪光灯
     */
    public void openCloseFlash() {
        if(isFlash){
            isFlash = false;
            mZXingView.closeFlashlight(); // 关闭闪光灯
            mIvFlash.setImageResource(R.mipmap.ic_flash_close);
            mTvFlashTip.setText("打开闪光灯");
        }else{
            isFlash = true;
            mZXingView.openFlashlight(); // 打开闪光灯
            mIvFlash.setImageResource(R.mipmap.ic_flash_open);
            mTvFlashTip.setText("关闭闪光灯");
        }
    }
}


